$regfile = "REG51.dat"

PORT7SEG alias P3
EN_HIGH alias P1.1
EN_LOW alias P1.2
SENSOR1 alias P2.7
SENSOR2 alias P2.6

Dim iNumHigh as Byte , iNumLow as Byte , Digit as Byte

Config Timer0 = Timer , Gate = Internal , Mode = 2
'Timer0 = counter : timer0 operates as a counter
'Gate = Internal  : no external gate control
'Mode = 2         : 8-bit auto reload (default)

On Timer0 Timer_0_int
Load Timer0 , 100                                             'when the timer reaches 100 an interrupt will occur
Enable Interrupts                                             'enable the use of interrupts
Enable Timer0                                                 'enable the timer


Rem Setting Of Priority
Priority Set Timer0                                           'highest priority
Start Timer0                                                  'start the timer

iNumHigh = 0
iNumLow = 1

Do
  Waitms 100
  if SENSOR1 = 1 then
     Waitms 100
     if SENSOR1 = 1 then
        while SENSOR1 = 1
            waitms 100
        wend
        if iNumLow = 9 then
           iNumHigh = iNumHigh + 1
           iNumLow = 0
        else
            iNumLow = iNumLow + 1
        end if
     end if
  elseif SENSOR2 = 1 then
     Waitms 100
     if SENSOR2 = 1 then
        while SENSOR2 = 1
            waitms 100
        wend
        if iNumLow = 0 then
           if iNumHigh = 0 then
              iNumHigh = 0
              iNumLow = 0
           else
              iNumHigh = iNumHigh - 1
              iNumLow = 9
           end if
        else
            iNumLow = iNumLow - 1
        end if
     end if

  end if
Loop

End

Rem The Interrupt Handler For The Timer0 Interrupt
Timer_0_int:
  'we will display the valueof gt on the 7 segment displays
  Reset EN_HIGH                                               'all displays off
  Reset EN_LOW
  Digit = Digit + 1                                           'which display are we doing?
  If Digit > 2 Then Digit = 1

  If Digit = 1 Then
     PORT7SEG = Lookup(iNumHigh , 7seg)
     Set EN_HIGH
  Elseif Digit = 2 Then
     PORT7SEG = Lookup(iNumLow , 7seg)
     Set EN_LOW
  End If

Return


'here we store the values that will be assigned to P1 to show a number
'dont forget to add the other digit values!
7seg:
Data 63 , 6 , 155 , 143 , 4 , 12 , 16 , 6 , 32 , 7